package com.newsnetwork.article.controller;

import com.newsnetwork.api.BaseController;
import com.newsnetwork.api.controller.article.CommentControllerApi;
import com.newsnetwork.article.service.CommentPortalService;
import com.newsnetwork.grace.result.CommonJSONResult;
import com.newsnetwork.pojo.bo.CommentReplyBO;
import com.newsnetwork.pojo.vo.AppUserVO;
import com.newsnetwork.utils.PagedGridResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author DingNing
 * @date 2022/11/16 15:40
 * @Description: 评论相关业务的controller
 */
@RestController
public class CommentController extends BaseController implements CommentControllerApi {

    final static Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private CommentPortalService commentPortalService;
    @Override
    public CommonJSONResult createComment(CommentReplyBO commentReplyBO) {

        // 0. 判断BindingResult是否保存错误的验证信息，如果有，则直接return
//        if (result.hasErrors()) {
//            Map<String, String> errorMap = getErrors(result);
//            return CommonJSONResult.errorMap(errorMap);
//        }

        // 1. 根据留言用户的id查询他的昵称，用于存入到数据表进行字段的冗余处理，从而避免多表关联查询的性能影响
        String userId = commentReplyBO.getCommentUserId();

        // 2. 发起restTemplate调用用户服务，获得用户侧昵称
        Set<String> idSet = new HashSet<>();
        idSet.add(userId);
        AppUserVO userVO = getBasicUserList(idSet).get(0);
        String nickname = userVO.getNickname();
        String face = userVO.getFace();

        // 3. 保存用户评论的信息到数据库
        commentPortalService.createComment(commentReplyBO.getArticleId(),
                commentReplyBO.getFatherId(),
                commentReplyBO.getContent(),
                userId,
                nickname,
                face);

        return CommonJSONResult.ok();
    }

    @Override
    public CommonJSONResult commentCounts(String articleId) {

        Integer counts = getCountsFromRedis(REDIS_ARTICLE_COMMENT_COUNTS + ":" + articleId);

        return CommonJSONResult.ok(counts == null ? 0 : counts);
    }

    @Override
    public CommonJSONResult list(String articleId, Integer page, Integer pageSize) {
        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = commentPortalService.queryArticleComments(articleId, page, pageSize);
        return CommonJSONResult.ok(gridResult);
    }

    @Override
    public CommonJSONResult mng(String writerId, Integer page, Integer pageSize) {

        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }

        PagedGridResult gridResult = commentPortalService.queryWriterCommentsMng(writerId, page, pageSize);
        return CommonJSONResult.ok(gridResult);
    }

    @Override
    public CommonJSONResult delete(String writerId, String commentId) {
        commentPortalService.deleteComment(writerId, commentId);
        return CommonJSONResult.ok();
    }
}
